// LXH_DataIO.cpp: implementation of the CLXH_DataIO class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LXH_DataIO.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
char *ID_Check = "HLX";

CDataIO::CDataIO()
{
	FileBuf = new char [FILE_BUF_SIZE];
	if( DFile.setbuf(FileBuf,FILE_BUF_SIZE) == NULL )
	{
		MessageBox(NULL, "Can not set buffer for classe CDataIO", "Error",MB_OK | MB_ICONEXCLAMATION);
		exit(0);
	}
}

CDataIO::~CDataIO()
{
	if( DFile.is_open() ) DFile.close();
	delete[] FileBuf;
}

int CDataIO::PutData(CData &data)
{
	if( data.fMat == NULL ) return 0;
	int i;
	int nCount = 1;

	DFile.write( (char *)&data.nRow, sizeof(int) );
	DFile.write( (char *)&data.nCol, sizeof(int) );
	DFile.write( (char *)&data.nID, sizeof(int) );

	for( i = 0; i<data.nRow; i++ )
		DFile.write( (char *)((data.fMat)[i]), data.nCol * sizeof(float));

	CData *p = data.pNext;
	while( p != NULL )
	{
		DFile.write( (char *)&(p->nRow), sizeof(int));
		DFile.write( (char *)&(p->nCol), sizeof(int));
		DFile.write( (char *)&(p->nID) , sizeof(int) );
		for( i = 0; i < p->nRow; i++ )
			DFile.write( (char *)((p->fMat)[i]), p->nCol * sizeof(float));
		p = p->pNext;
		nCount++;
	}
	return nCount;	
}


CData * CDataIO::GetAllRec()
{
	CData *pNewData = new CData;
	CData *data;

	int i;

	while(!DFile.eof())
	{
		data = new CData;
		DFile.read((char *)&(data->nRow), sizeof(int));
		if(DFile.eof()) 
		{
			delete data;
			break;
		}

		DFile.read((char *)&(data->nCol), sizeof(int));
		DFile.read((char *)&(data->nID) , sizeof(int));
		data->pNext = NULL;

		data->fMat = CData::New_FMatrix( data->nRow, data->nCol );
		for( i = 0; i < data->nRow; i++ )
			DFile.read((char *)((data->fMat)[i]), data->nCol * sizeof(float));
		pNewData->Append( data );
	}
	return pNewData;
}


void CDataIO::OpenFile(char *FName, int Mode)
{
	if(DFile.is_open()) ErrorMsg( "You must call CloseFile() before reuse this function");
	char *Info=INFO;

	if( Mode & WRITE_REC )
	{
		DFile.open(FName, ios::in | ios::binary, filebuf::sh_read);
		if( DFile.fail() )
		{
			char msg[255];
			sprintf(msg,"Error in OpenFile : %s not found !", FName);
			ErrorMsg( msg );
		}
		//Check file ID
		char temp[3];
		DFile.read(temp, 3);
		if( DFile.eof() )
		{
			DFile.close();
			DFile.open(FName, ios::app | ios::out | ios::binary, filebuf::sh_read);
			DFile.write(ID_Check, strlen(ID_Check));
			DFile.write(Info, strlen(Info));
		}
		else
		{
			if(  (temp[0] != 'H') || (temp[1]!= 'L') || (temp[2] != 'X') )
			{
				char msg[255];
				sprintf(msg,"%s : is not created by CDataIO !", FName);
				ErrorMsg( msg );
			}
			else
			{
				DFile.close();
				DFile.open(FName, ios::app | ios::out | ios::binary, filebuf::sh_read);
			}
		}
		
	}
	else
	{
		if( Mode & READ_REC )
		{
			DFile.open(FName, ios::in | ios::binary | ios::nocreate, filebuf::sh_read);

			if( DFile.fail() )
			{
				char msg[255];
				sprintf(msg,"%s : is not existed !", FName);
				MessageBox(NULL, msg, "Error", MB_OK | MB_ICONEXCLAMATION);
				exit(0);
			}

			//Check file ID
			char temp[3];
			DFile.read(temp, 3);
			if( (temp[0] != 'H') || (temp[1]!= 'L') || (temp[2] != 'X') )
			{
				char msg[255];
				sprintf(msg,"%s : is not created by CDataIO !", FName);
				ErrorMsg( msg );
			}
			DFile.seekg(strlen(ID_Check) + strlen(Info));
		}
		else
			ErrorMsg("You must only use WRITE_REC or READ_REC for the parameter : Mode");
	}
}


CData * CDataIO::GetDataRec()
{
	CData *data = new CData();
	
	DFile.read((char *)&(data->nRow), sizeof(int));
	if(DFile.eof()) 
	{
		delete data;
		return NULL;
	}

	DFile.read((char *)&(data->nCol), sizeof(int));
	DFile.read((char *)&(data->nID) , sizeof(int));
	data->Alloc();

	for(int i = 0; i < data->nRow; i++ )
		DFile.read((char *)((data->fMat)[i]), data->nCol * sizeof(float));
	return data;
}


void CDataIO::CloseFile()
{
	DFile.close();
}
